// Utility.keyConvert();
// const smcId = hardware.smartCard.serialNumber;
// EVM.cookie.setValue('smcId', smcId);
var indexR = 0;
var data = [{
        name: "登入",
        url: "1.html",
    },
    {
        name: "视频",
        url: "2.html",
    },
    {
        name: "满意调查",
        url: "3.html",
    },
    {
        name: "关于我们",
        url: "about-us.html",
    },
    {
        name: "时政要闻",
        url: "shizheng-list.html",
    },
    {
        name: "办事指南",
        url: "work-guide-list.html",
    },
    {
        name: "行业动态",
        url: "industry-list.html",
    },
    {
        name: "教育培训",
        url: "jiaoyu-list.html",
    },
    {
        name: "娱乐活动",
        url: "yule-list.html",
    },
];
var hasIndex = EVM.cookie.getValue('sdzIndex');
console.log(hasIndex, 'hasIndex');




//获取机顶盒或浏览器信息
var env = evm.getStbBrowser();

function cookie(k, v) {
    if (v != undefined && typeof v == 'string') {
        evm.cookie(k, v);
        return;
    }
    if (!!evm.cookie(k)) {
        return evm.cookie(k);
    }
}
/*
 * $cmsIndex首页视图模块
 * 依赖:
 */
var cmsIndex = (function () {
    var mod = {};

    //配置项
    var config = mod.config = {
        //开启cookie开关
        cookieSwitch: true,
        cookieKey: 'cmsIndex',
        //模拟数据配置
        mockData: {
            //URI查询标示符
            key: '[mdata]',
            //文件目录
            dir: '../source/mockData'
        },
        //调试输出开关
        debug: false
    };

    var localData = mod.localData = {
        indexSource: '[mdata]/index.js'
    };
    //接口数据缓存
    var jsonCache = {};

    //各模块焦点切换的配置
    var modules = mod.modules = [];

    //当前模块的索引值,被引用的模块
    moduleId = 0,
        module = null,
        moduleGuid = 0,
        moduleCount = -1;

    mod.addModule = function () {
        return moduleCount += 1;
    };

    //moduleGuid存取器
    mod.moduleGuid = function (id) {
        if (typeof id == 'number') moduleGuid = id;
        return moduleGuid;
    };

    if (config.cookieSwitch && config.cookieKey) {
        //read cookie
        var pageCookie = cookie(config.cookieKey);
        if (pageCookie) {
            pageCookie = pageCookie.split(',');

            if (pageCookie.length > 0) {
                var moduleCookie = mod.moduleCookie = pageCookie[0].split('.');

                if (pageCookie[1]) {
                    var viewportCookie = mod.viewportCookie = moduleCookie;
                    moduleCookie = mod.moduleCookie = pageCookie[1].split('.');

                }
                if (pageCookie[2]) {
                    var parentCookie = mod.parentCookie = pageCookie[2].split('.');
                }
            }
        }

        //清除cookie
        evm.cutCookie(config.cookieKey);
    }
    //添加视图按键管理器
    mod.addListener = function () {
        evm.controller.subscribe({
            type: 1,
            method: 'add',
            callback: mod.grabEvent,
            context: mod
        });
    };
    // console.log(mod)
    //移除视图按键管理器
    mod.removeListener = function () {
        evm.controller.subscribe({
            type: 1,
            method: 'remove',
            callback: mod.grabEvent,
            context: mod
        });
    };
    // console.log(module)
    //视图按键管理

    mod.grabEvent = function (key) {
        var me = mod;
        //拦截返回键,方法返回isBlock是1或0,默认0不拦截
        var isBlock = 0;

        if (module && module.grabEvent) {
            isBlock = module.grabEvent(key) || 0;
        }

        return isBlock;
    };
    //模块存取器
    mod.module = function (mID) {
        if (modules[mID]) {
            module = modules[mID].module || null;
        }

        return module;
    };

    //切换当前模块
    mod.handle = function (directive) {
        var me = mod;
        if (modules[moduleId].map &&
            (typeof modules[moduleId].map[directive] == 'number')) {

            moduleId = modules[moduleId].map[directive];

            return me.module(moduleId);
        }
    };
    mod.paint = function () {
        var me = mod;
        me.addListener();
        return me;
    };

    mod.run = function () {
        var me = mod;
        //默认moduleId = 0 导航栏
        //cookie得到焦点

        var nav = mod.module(moduleId);
        cmsIndex.handle('turntable');
        turntable.toggleFocus();
        //控制器管理按键
        evm.on(document, (!env.stb ? 'keydown' : (env.stb == 'iPanel' ?
            'irkeypress' : 'keydown')), evm.controller, evm);

        me.paint();

        return me;
    };

    return mod;

})();


/*
 * 1.右侧区
 */

cmsIndex.modules[cmsIndex.moduleGuid(cmsIndex.addModule())] = {
    modName: 'right',
    modNum: cmsIndex.moduleGuid(),
    map: {
        left: 1
    }
};

var right = cmsIndex.modules[cmsIndex.moduleGuid()].module = (function () {
    var mod = {},
        index = 1, //右侧选中地址位置
        listName = 'model',
        length = 9,
        containerId = 'right',
        checkCookie = true; //配置是否定位焦点到二级栏目，默认否
    mod.modName = 'huodong';
    mod.modNum = cmsIndex.moduleGuid();
    mod.toggleFocus = function (hasIndex) {
        var me = mod;
        var elem = evm.$('model_container1').getElementsByClassName(listName);
        for (i = 0; i < length; i++) {
            elem[i].className = listName;
        }
        if (hasIndex) {
            index = parseInt(hasIndex);
            // EVM.cookie.deleteValue('sdzIndex');
            hasIndex = '';
            if (index != length) {
                if (document.getElementsByClassName("active").length > 0) {
                    document.getElementsByClassName("active")[0].className = "model";
                }
            } else {
                index = 0;
            }
        }
        if (index >= 0) {
            elem[index].className = elem[index].className + ' active';
        }
        return me;
    };

    mod.clearCss = function () {
        var me = mod;
        elem = $('.' + listName);
        for (i = 0; i < 9; i++) {
            elem[i].className = listName;
        }
        return me;
    };
    //赋值


    mod.moveH = function (directive) {
        var me = mod;
        if (directive == "backwards") { //左
            if (index == 2) {
                mod.clearCss();
                cmsIndex.handle('left');
                left.toggleFocus(0);
            } else if (index == 0) {
                index = 0;
                mod.toggleFocus();
            } else if (index == 1 || index == 3 || index == 4 || index == 5 || index == 6 || index == 7 || index == 8) {
                index -= 1;
                mod.toggleFocus();
            }
        } else if (directive == 'forwards') { //右
            if (index == 1) {
                right.clearCss();
                cmsIndex.handle('left');
                left.toggleFocus(0);
            } else if (index == 8) {
                index = 8;
                mod.toggleFocus();
            } else if (index == 0 || index == 2 || index == 3 || index == 4 || index == 5 || index == 6 || index == 7) {
                index += 1;
                mod.toggleFocus();
            }
        }

        // mod.toggleFocus();
        return me;
    };
    mod.moveU = function (directive) {
        var me = mod;
        if (directive == "up") { //上
            if (index == 0 || index == 1 || index == 2) {
                index = 0
                mod.toggleFocus();
            } else if (index == 3) {
                index = 2
                mod.toggleFocus();
            } else if (index == 4 || index == 5) {
                index = 1
                mod.toggleFocus();
            } else if (index == 6 || index == 7) {
                right.clearCss()
                cmsIndex.handle('left')
                left.toggleFocus(4)
            } else if (index == 8) {
                index = 3;
                mod.toggleFocus();
            }
        } else if (directive == 'down') { //下
            if (index == 0) {
                index = 2
            } else if (index == 2) {
                index = 3
            } else if (index == 3) {
                index = 8
            } else if (index == 1) {
                index = 4
            }
            mod.toggleFocus();
        }

        return me;
    };
    mod.action = function (directive) {
        var me = mod;
        switch (directive) {
            case 'back':
                break;
            case 'enter':
                // EVM.cookie.setValue('sdzIndex',index);
                window.location.href = data[index].url;
                break;
        }
        return me;
    };
    mod.grabEvent = function (key) {
        var me = mod;
        //拦截返回键,方法返回isBlock是1或0,默认0不拦截
        var isBlock = 0;
        switch (key) {
            case keymap.Enrich.UP:
            case keymap.iPanel.UP:
            case keymap.DVN.UP:
                me.moveU('up');
                break;
            case keymap.Enrich.DOWN:
            case keymap.iPanel.DOWN:
            case keymap.DVN.DOWN:
                me.moveU('down');
                break;
            case keymap.Enrich.LEFT:
            case keymap.iPanel.LEFT:
            case keymap.DVN.LEFT:
                me.moveH('backwards');

                break;
            case keymap.Enrich.RIGHT:
            case keymap.iPanel.RIGHT:
            case keymap.DVN.RIGHT:

                me.moveH('forwards');

                break;
            case keymap.SELECT:
            case keymap.SELECT2:
                me.action('enter');
                break;
            case keymap.Enrich.BACK:
            case keymap.iPanel.BACK:
            case keymap.DVN.BACK:
            case keymap.Other.BACK:
                me.action('back');
                break;
            case keymap.Inspur.EXIT:
            case keymap.iPanel.EXIT:
            case 0:
                window.location.href = "address.html";
                break;
        }
        return isBlock;
    };

    return mod;
})();


cmsIndex.modules[cmsIndex.moduleGuid(cmsIndex.addModule())] = {
    modName: 'left',
    modNum: cmsIndex.moduleGuid(),
    map: {
        right: 0
    }
};

var left = cmsIndex.modules[cmsIndex.moduleGuid()].module = (function () {
    var mod = {},
        index = 0, //右侧选中地址位置
        listName = 'model2',
        length = 5,
        containerId = 'left',
        checkCookie = true; //配置是否定位焦点到二级栏目，默认否
    mod.modName = 'left';
    mod.modNum = cmsIndex.moduleGuid();
    mod.toggleFocus = function () {
        var me = mod;
        var elem = evm.$('model_container2').getElementsByClassName(listName);
        for (i = 0; i < length; i++) {
            elem[i].className = listName;
        }
        if (hasIndex) {
            index = parseInt(hasIndex);
            // EVM.cookie.deleteValue('sdzIndex');
            hasIndex = '';
            if (index != length) {
                if (document.getElementsByClassName("active").length > 0) {
                    document.getElementsByClassName("active")[0].className = "model2";
                }
            } else {
                index = 0;
            }
        }
        if (index >= 0) {
            elem[index].className = elem[index].className + ' active';
        }
        return me;
    };

    mod.clearCss = function () {
        var me = mod;
        elem = $('.model_container2>.' + listName);
        for (i = 0; i < 5; i++) {
            elem[i].className = listName;
        }
        return me;
    };
    //赋值


    mod.moveH = function (directive) {
        var me = mod;
        if (directive == "backwards") { //左
            left.clearCss();
            cmsIndex.handle('right');

            right.toggleFocus(1);

        } else if (directive == 'forwards') { //右
            left.clearCss();
            cmsIndex.handle('right');
            right.toggleFocus(2);
        }
        return me;
    };
    mod.moveU = function (directive) {
        var me = mod;
        if (directive == "up") { //上
            if (index == 1 || index == 2 || index == 3 || index == 4) {
                index -= 1;
                mod.toggleFocus();
            } else if (index == 0) {
                left.clearCss();
                cmsIndex.handle('right');
                right.toggleFocus(0);
            }
        } else if (directive == 'down') { //下
            if (index == 4) {
                left.clearCss();
                cmsIndex.handle('right');
                right.toggleFocus(6)
            } else {
                index += 1;
                mod.toggleFocus();
            }
        }
        return me;
    };
    mod.action = function (directive) {
        var me = mod;
        switch (directive) {
            case 'back':
                break;
            case 'enter':
                EVM.cookie.setValue('sdzIndex', index);
                window.location.href = data[index].url;
                break;
        }
        return me;
    };
    mod.grabEvent = function (key) {
        var me = mod;
        //拦截返回键,方法返回isBlock是1或0,默认0不拦截
        var isBlock = 0;
        switch (key) {
            case keymap.Enrich.UP:
            case keymap.iPanel.UP:
            case keymap.DVN.UP:
                me.moveU('up');
                break;
            case keymap.Enrich.DOWN:
            case keymap.iPanel.DOWN:
            case keymap.DVN.DOWN:
                me.moveU('down');
                break;
            case keymap.Enrich.LEFT:
            case keymap.iPanel.LEFT:
            case keymap.DVN.LEFT:
                me.moveH('backwards');

                break;
            case keymap.Enrich.RIGHT:
            case keymap.iPanel.RIGHT:
            case keymap.DVN.RIGHT:

                me.moveH('forwards');

                break;
            case keymap.SELECT:
            case keymap.SELECT2:
                me.action('enter');
                break;
            case keymap.Enrich.BACK:
            case keymap.iPanel.BACK:
            case keymap.DVN.BACK:
            case keymap.Other.BACK:
                me.action('back');
                break;
            case keymap.Inspur.EXIT:
            case keymap.iPanel.EXIT:
            case 0:
                window.location.href = "address.html";
                break;
        }
        return isBlock;
    };

    return mod;
})();










cmsIndex.run();